#include"linkList.h"


ListNode* BuyNode(DateType x)
{
    ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
    if(newnode == NULL)
    {
        perror("malloc:");
        exit(1);
    }
    newnode->next = NULL;
    newnode->val = x;
    return newnode;
}
void ListPrint(ListNode* phead)
{
    while(phead)
    {
        printf("%d ",phead->val);
        phead = phead->next;
    }
    printf("\n");
}
void ListPushBack(ListNode** phead,DateType x)
{
    assert(phead);
    ListNode* newnode = BuyNode(x);
    if((*phead) == NULL)//链表为空
    {
        (*phead) = newnode;
    }
    else
    {
        ListNode* tail = *phead;//找尾节点
        while(tail->next)
            tail = tail->next;
        tail->next = newnode;
    }
}
void ListPopBack(ListNode** phead)
{
    assert(phead);
    assert(*phead);//防止链表为空
    if((*phead)->next == NULL) //只有一个节点
    {
        free(*phead);
        *phead = NULL;
    }
    else
    {   
        ListNode* tail = *phead;
        ListNode* prev = NULL;
        while(tail->next)
        {
            prev = tail;
            tail = tail->next;
        }
        prev->next = NULL;
        free(tail);
        tail = NULL;
    }
}
void ListPushFront(ListNode** phead,DateType x)
{
    assert(phead);
    ListNode* newnode = BuyNode(x);
    //链表为空 || 只有一个节点等情况都适用
    newnode->next = (*phead);
    *phead = newnode;
}
void ListPopFront(ListNode** phead)
{
    assert(phead);
    assert(*phead);//防止链表为空
    ListNode* next = (*phead)->next;
    free(*phead);
    *phead = next;
}
ListNode* findNode(ListNode* phead,DateType x)
{
    while(phead)
    {
        if(phead->val == x)
            return phead;
        phead = phead->next;
    }
    return NULL;
}
void InsertAfterPos(ListNode* pos,DateType x)//在pos后插入  不会改变pos位置的指向，所以传入一级指针即可
{
    ListNode* posNext = pos->next;
    ListNode* newnode = BuyNode(x);
    //*pos newnode posNext
    pos->next = newnode;
    newnode->next = posNext; 
}
//在pos前插入 有可能就是在头结点前插入,此时需要更换头节点
void InsertfrontPos(ListNode** phead,ListNode* pos,DateType x)
{
    assert(phead && pos);
    ListNode* newnode  = BuyNode(x);
    if(pos == (*phead))
    {
        newnode->next = *phead;
        (*phead) = newnode;
    }
    else
    {
        ListNode* posPrev = *phead;//找到pos前一个节点
        while(posPrev->next != pos)
            posPrev = posPrev->next;
        //posPrev newnode pos
        posPrev->next = newnode;
        newnode->next = pos;
    }
}
//删除pos位置节点,有可能删除的就是头结点
void ErasePos(ListNode** phead,ListNode* pos)
{
    assert(pos && phead &&(*phead) );//保证链表不为空
    if( (*phead) == pos )
    {
        ListNode* next = (*phead)->next;
        free(*phead);
        *phead = next;
    }
    else
    {
        ListNode* posPrev = NULL;
        ListNode* cur = *phead;
        ListNode* posNext = pos->next;
        while(cur != pos)
        {
            posPrev = cur;
            cur = cur->next;
        }
        free(pos);
        posPrev->next = posNext;
    }
}
void EraseAfterPos(ListNode* pos) //删除pos位置后的节点 不会改变pos位置的指向，所以传入一级指针即可
{
    assert(pos && pos->next );//pos不能是尾节点
    ListNode* next = pos->next->next;
    free(pos->next);
    pos->next = next;

}
void ListDestroy(ListNode** phead)
{
    ListNode* cur = *phead;
    ListNode* next = NULL;
    while(cur)
    {
        next = cur->next;
        free(cur);
        cur = next;
    }
    *phead = NULL;
}
